package au.com.iglootech.TaxTimeToolTip.ui.fillupform;

import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.Entity.Company;
import au.com.iglootech.TaxTimeToolTip.Entity.Task;
import au.com.iglootech.TaxTimeToolTip.service.TaskService;
import au.com.iglootech.TaxTimeToolTip.ui.company.CreateCompanyTab;
import au.com.iglootech.TaxTimeToolTip.ui.company.ManageCompanyTab;
import au.com.iglootech.TaxTimeToolTip.util.FeeUtil;
import au.com.iglootech.component.IGTTable;

import com.google.appengine.api.datastore.Key;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.event.Action;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
@Configurable(preConstruction=true)
@Component
@Scope("request")
public class TodayTimeSheetTab extends CustomComponent {
	static final Action ACTION_MARK = new Action("Mark");
    static final Action ACTION_UNMARK = new Action("Unmark");
    static final Action ACTION_LOG = new Action("Save");
    static final Action[] ACTIONS_UNMARKED = new Action[] { ACTION_MARK,
            ACTION_LOG };
    static final Action[] ACTIONS_MARKED = new Action[] { ACTION_UNMARK,
            ACTION_LOG };
    
	VerticalLayout mainLayout = new VerticalLayout();
	@Autowired
	private transient TaskService taskService;
	private HashSet<Object> markedRows = new HashSet<Object>();
	private IGTTable taskTable;  
	private BeanContainer<String, Task> beans;
	private Label title;

	public TodayTimeSheetTab() {
		setCaption("Today's tasks");
		initContent();
	}

	private void initContent() {
		Panel panel = new Panel();
		
		beans = new BeanContainer<String, Task>(
				Task.class);
		beans.addNestedContainerProperty("company.companyName");
		// Use the key property as the item ID of the bean
		beans.setBeanIdProperty("key");

		List<Task> tasks = taskService.findTodayTasks();

		int periodCount = 0;
		double unitCount = 0;
		int count=0;
		double feeCount = 0;
		for(Task task : tasks)
		{
			beans.addBean(task);
			count++;
			periodCount+=task.getPeriod();
			unitCount += task.getUnit();
			feeCount += task.getPeriod() * task.getUnit();
		}
		unitCount = unitCount/count;
		title = new Label("Today: " + FeeUtil.formatFee(feeCount) + "$");
		title.addStyleName("menu");
		panel.addComponent(title);
		taskTable = new IGTTable("Task List", beans);
		taskTable.setSelectable(true);
		taskTable.setImmediate(true); // react at once when something is selected
		taskTable.addListener(new Property.ValueChangeListener() {
		    public void valueChange(ValueChangeEvent event) {
		        EditTimeItemWindow editWindow = new EditTimeItemWindow((Key)taskTable.getValue());
		        getWindow().addWindow(editWindow);
		    }
		});
		taskTable.setWidth("100%");
		taskTable.setHeight("500px");
		taskTable.setColumnHeader("company.companyName", "Company");
		taskTable.setVisibleColumns(new String[]{"taskName", "company.companyName", "period", "unit", "invoiced", "startDate"});
		taskTable.setFooterVisible(true);
		taskTable.setColumnFooter("taskName", "Count");
		taskTable.setColumnFooter("period", String.valueOf(periodCount));
		taskTable.setColumnFooter("unit", FeeUtil.formatFeeWithUnit(feeCount));

		panel.addComponent(taskTable);
		mainLayout.addComponent(panel);
		this.setCompositionRoot(mainLayout);
	}
	
	public void refresh()
	{
		List<Task> tasks = taskService.findAll();
		beans.removeAllItems();
		int periodCount = 0;
		double unitCount = 0;
		int count=0;
		double feeCount = 0;
		for(Task task : tasks)
		{
			beans.addBean(task);
			count++;
			periodCount+=task.getPeriod();
			unitCount += task.getUnit();
			feeCount += task.getPeriod() * task.getUnit();
		}
		unitCount = unitCount/count;
		title.setValue("Today: " + FeeUtil.formatFee(feeCount) + "$");
		taskTable.setColumnFooter("taskName", "Count");
		taskTable.setColumnFooter("period", String.valueOf(periodCount));
		taskTable.setColumnFooter("unit", FeeUtil.formatFeeWithUnit(feeCount));
	}
}
